﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;

namespace QueryResource.TextFormat
{
    /// <summary>
    /// 自动换行格式刷
    /// </summary>
    internal class AutoNewLineFormat : ITextFormat
    {
        private const int Margins = 30;                    //格式化时 右边距
        public AutoNewLineFormat(double width, string fontName, double fontSize)
        {
            this.MaxWidth = width - Margins;
            this.FontName = fontName;
            this.FontSize = fontSize;
        }
        public double MaxWidth { get; set; }
        public string FontName { get; set; }
        public double FontSize { get; set; }

        #region ITextFormat 成员

        public string TextFormat(string text)
        {
            if (this.MaxWidth <= 0)
                return text;

            int rowBeginIndex = 0;
            int textLength = text.Length;
            StringBuilder sb = new StringBuilder();

            for (int index = 0; index < textLength; index++)
            {
                int rowEndIndex = index;

                if (index == textLength - 1)
                {
                    sb.Append(text.Substring(rowBeginIndex));
                    //sb.Append(Environment.NewLine);
                }
                else if (rowEndIndex + 1 < text.Length && text.Substring(rowEndIndex, 2) == Environment.NewLine)
                {
                    //这里是防止 添加2次换行
                    if (rowEndIndex - rowBeginIndex != 0)
                    {
                        string context = text.Substring(rowBeginIndex, rowEndIndex - rowBeginIndex);
                        if (context.Trim().Length != 0)
                        {
                            sb.Append(context);
                            sb.Append(Environment.NewLine);
                        }
                    }
                    rowEndIndex = ++index;
                    rowBeginIndex = rowEndIndex + 1;
                }
                else if (MeasureTextWidth(text.Substring(rowBeginIndex, rowEndIndex - rowBeginIndex + 1)) > this.MaxWidth)
                {
                    int end = rowEndIndex - rowBeginIndex;
                    string content = text.Substring(rowBeginIndex, end);

                    //防止 在字段中间给换行。造成查询数据库时错误
                    //查找最后一个逗号或空格 作为分界线
                    List<int> list = new List<int>
                                         {
                                             content.LastIndexOf(','), 
                                             content.LastIndexOf(' '),
                                             content.LastIndexOf('-'),
                                             content.LastIndexOf('+'),
                                             content.LastIndexOf('*')
                                         };
                    int max = list.Max();
                    if (max <= 0)
                    {
                        sb.Append(content);
                        rowBeginIndex = rowBeginIndex + end;
                    }
                    else
                    {
                        sb.Append(text.Substring(rowBeginIndex, max));
                        rowBeginIndex = rowBeginIndex + max;
                    }
                    sb.Append(Environment.NewLine);
                    index = rowBeginIndex;
                }
            }
            return sb.ToString();
        }

        #endregion

        /// <summary>
        /// 获取字符串的宽度
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        private double MeasureTextWidth(string text)
        {
            FormattedText formattedText = new FormattedText(
            text,
            System.Globalization.CultureInfo.InvariantCulture,
            System.Windows.FlowDirection.LeftToRight,
            new Typeface(this.FontName),
            this.FontSize,
            Brushes.Black
            );
            return formattedText.WidthIncludingTrailingWhitespace;
        }
    }
}
